/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.testthread.test.nnn;

class Resource
    {
    	private String name;
    	private int count = 1;
    	private boolean flag = false;
    	public synchronized void set(String name)//  t0   t1
    	{
    		while(flag)
    			try{this.wait();}catch(InterruptedException e){} //若线程在此处被唤醒，需要回头重新判断标记
    		
    		this.name = name + count;//烤鸭1  烤鸭2  烤鸭3
    		count++;//2 3 4
    		System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);//生产烤鸭1 生产烤鸭2 生产烤鸭3
    		flag = true;
    		notify();
    	}
    
    	public synchronized void out()
    	{
    		while(!flag)
    			try{this.wait();}catch(InterruptedException e){}	//t2  t3
    		System.out.println(Thread.currentThread().getName()+"...消费者........"+this.name);//消费烤鸭1
    		flag = false;
    		notify();
    	}
    }